package com.liang.index334.core;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.liang.index334.domain.*;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author liangyehao
 * @version 1.0
 * @date 2021/4/30 23:23
 * @content
 */
public class Director {


    public static String doWork(String filePath, File basicFile, String month) throws FileNotFoundException {

        int[] monDays = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int days = monDays[Integer.parseInt(month)-1];


        List<CodeResult> sumCodeData = readAndSumCodeData(basicFile,days);
        List<NewResult> sumNewData = readAndSumNewData(basicFile);
        List<VrResult> sumVrData = readAndSumVrData(basicFile);

        List<AgentResult> agentResults = AgentResult.build(sumCodeData, sumNewData, sumVrData);

        List<StoreResult> storeResults = sumByStoreGroup(agentResults);


        //格式化格式
        String format = "YYYYMMddHHmmss";
        // DateTimeFormatter.ofPattern方法根据指定的格式输出时间
        String formatDateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern(format));

        String fileName = filePath+"334指标" + formatDateTime + ".xlsx";

        FileOutputStream outputStream = new FileOutputStream(fileName);
        ExcelWriter excelWriter = EasyExcel.write(outputStream).build();

        WriteSheet mainSheet = EasyExcel.writerSheet(0, "店组334").head(StoreResult.class).build();
        excelWriter.write(storeResults,mainSheet);

        WriteSheet sheet1 = EasyExcel.writerSheet(1, "经纪人334").head(AgentResult.class).build();
        excelWriter.write(agentResults,sheet1);

        excelWriter.finish();


        return fileName;
    }

    private static List<StoreResult> sumByStoreGroup(List<AgentResult> agentResults) {
        List<StoreResult> storeResults = new ArrayList<>();

        Map<String, List<AgentResult>> collect = agentResults.stream()
                .collect(Collectors.groupingBy(AgentResult::getStoresGroup));
        for (Map.Entry<String, List<AgentResult>> entry : collect.entrySet()) {
            String storesGroup = entry.getKey();
            List<AgentResult> agentResultList = entry.getValue();

            int total = agentResultList.size();
            int codeNotFlunk = (int) agentResultList.stream().filter(agentResult -> "预警关黑".equals(agentResult.getCodeFlunk())).count();
            int newNotFlunk = (int) agentResultList.stream().filter(agentResult -> "预警关黑".equals(agentResult.getPullNewFlunk())).count();
            int vrNotFlunk = (int) agentResultList.stream().filter(agentResult -> "预警关黑".equals(agentResult.getVrFlunk())).count();


            StoreResult storeResult = new StoreResult();
            storeResult.setStoresGroup(storesGroup);
            storeResult.setCodeFlunk(total-codeNotFlunk);
            storeResult.setCodeNotFlunk(codeNotFlunk);
            storeResult.setNewFlunk(total-newNotFlunk);
            storeResult.setNewNotFlunk(newNotFlunk);
            storeResult.setVrFlunk(total-vrNotFlunk);
            storeResult.setVrNotFlunk(vrNotFlunk);

            storeResults.add(storeResult);
        }

        return storeResults;
    }

    private static List<VrResult> readAndSumVrData(File basicFile) {

        List<VrResult> sumVrData = new ArrayList<>();

        // 读取数据
        VrDataListener dataListener = new VrDataListener();

        EasyExcel.read(basicFile, VrDetail.class, dataListener).sheet(2).doRead();

        List<VrDetail> list = dataListener.getList();

        Map<String, List<VrDetail>> agents = list.stream()
                .collect(Collectors.groupingBy(VrDetail::getAgentName));

        for (Map.Entry<String, List<VrDetail>> entry : agents.entrySet()) {
            String agentName = entry.getKey();
            List<VrDetail> details = entry.getValue();

            List<VrDetail> collect = details.stream().filter(detail ->
                    detail.getEffective() == 1 && !"D".equalsIgnoreCase(detail.getWithSeeLevel()))
                    .collect(Collectors.toList());

            VrResult vrResult = new VrResult();
            vrResult.setAgentName(agentName);
            vrResult.setVrAmount(collect.size());

            sumVrData.add(vrResult);

        }

        return sumVrData;
    }

    private static List<NewResult> readAndSumNewData(File basicFile) {
        List<NewResult> sumNewData = new ArrayList<>();

        // 读取数据
        NewDataListener dataListener = new NewDataListener();

        EasyExcel.read(basicFile, NewDetail.class, dataListener).sheet(1).doRead();

        List<NewDetail> list = dataListener.getList();

        Map<String, List<NewDetail>> agents = list.stream()
                .collect(Collectors.groupingBy(NewDetail::getAgentName));

        for (Map.Entry<String, List<NewDetail>> entry : agents.entrySet()) {
            String agentName = entry.getKey();
            List<NewDetail> details = entry.getValue();
            String jobs = details.get(0).getJobs();
            int sum = details.stream().mapToInt(NewDetail::getNewAmount).sum();

            NewResult newResult = new NewResult();
            newResult.setAgentName(agentName);
            newResult.setJobs(jobs);
            newResult.setNewAmount(sum);
            sumNewData.add(newResult);

        }

        return sumNewData;
    }

    private static List<CodeResult> readAndSumCodeData(File baseFile, int days) {
        List<CodeResult> sumCodeData = new ArrayList<>();

        // 读取数据
        CodeDataListener dataListener = new CodeDataListener();

        EasyExcel.read(baseFile, CodeDetail.class, dataListener).sheet(0).doRead();

        List<CodeDetail> list = dataListener.getList();

        Map<String, List<CodeDetail>> agents = list.stream()
                .collect(Collectors.groupingBy(CodeDetail::getAgentName));

        for (Map.Entry<String, List<CodeDetail>> entry : agents.entrySet()) {
            String agentName = entry.getKey();
            List<CodeDetail> details = entry.getValue();
            String stores = details.get(0).getStores();
            String storesGroup = details.get(0).getStoresGroup();
            int sum = details.stream().mapToInt(CodeDetail::getCodeAmount).sum();

            CodeResult codeResult = new CodeResult();
            codeResult.setAgentName(agentName);
            codeResult.setStores(stores);
            codeResult.setStoresGroup(storesGroup);
            codeResult.setCodeAmount(sum,days);
            sumCodeData.add(codeResult);
        }

        return sumCodeData;
    }

    public static void main(String[] args) throws FileNotFoundException {
        String s = "E:\\WorkSpace\\personal\\product\\beike-excel\\excel\\";
        doWork(s,new File(s+"334-basic.xlsx"),"4");
    }



}
